package ysq.xyg.web.controller;

import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.util.BaseConstant;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.controller.BaseController;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.po.WebProduct;
import ysq.xyg.web.model.po.WebTagContent;
import ysq.xyg.web.model.po.WebTagType;
import ysq.xyg.web.model.vo.WebProductVo;
import ysq.xyg.web.model.vo.WebTagContentVo;
import ysq.xyg.web.model.vo.WebTagTypeVo;
import ysq.xyg.web.service.WebProductService;
import ysq.xyg.web.service.WebTagContentService;
import ysq.xyg.web.service.WebTagTypeService;
import ysq.xyg.web.util.WebBaseContent;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 网站模块-商品信息controller层
 *
 * @author shengQiang yu
 * @date 2019-6-7 18:07:28
 */
@RestController
public class WebProductController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(WebProductController.class);
    @Resource
    private WebProductService webProductService;
    @Resource
    private WebTagContentService webTagContentService;
    @Resource
    private WebTagTypeService webTagTypeService;
    private WebProduct webProduct;

    /**
     * 分页查询
     *
     * @param webProductVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author shengQiang yu
     * @date 2019-6-7 18:07:28
     */
    @BaseLog(module = "网站模块",className = "商品信息",methods = "分页查询",businessType = BaseConstant.SYS_BUSINESS_TYPE_GET,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/webProduct/page",method = RequestMethod.POST)
    public ReturnEntity page(WebProductVo webProductVo, int pageSize, int pageNum){
        ReturnEntity returnEntity = webProductService.page(webProductVo,pageSize,pageNum);
        return returnEntity;
    }

    /**
     * 跳转连接
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-6-7 18:07:28
     */
    @RequestMapping(value = "/webProduct/getEntity",method = RequestMethod.GET)
    public ModelAndView getEntity(HttpServletRequest request,String id){
        ModelAndView view = new ModelAndView("/web/webProductUpdateOrSave");
        SysUser sysUser=acceptCurrentUser(request);
        List<WebTagType> webTagTypeList=webTagTypeService.acceptList(new WebTagTypeVo(WebBaseContent.web_tag_type_cp,BaseConstant.base_show_type_zs,sysUser.getId()));
        if(StrUtil.isNotEmpty(id)){
            webProduct = webProductService.selectByPrimaryKey(id);
            List<WebTagContent> webTagContentList=webTagContentService.acceptList(new WebTagContentVo("",WebBaseContent.web_tag_type_cp,id,sysUser.getId()));
            if(webTagContentList!=null&&webTagContentList.size()>0){
                for (WebTagContent webTagContent:webTagContentList){
                    if(StrUtil.isEmpty(webProduct.getTagIds())){
                        webProduct.setTagIds(webTagContent.getTagId());
                    }else{
                        webProduct.setTagIds(webProduct.getTagIds()+","+webTagContent.getTagId());
                    }
                }
            }
            view.addObject("webProduct", webProduct);
        }
        view.addObject("webTagTypeList", webTagTypeList);
        return view;
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-6-7 18:07:28
     */
    @BaseLog(module = "网站模块",className = "商品信息",methods = "删除",businessType = BaseConstant.SYS_BUSINESS_TYPE_DELETE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/webProduct/delete")
    public ReturnEntity delete(HttpServletRequest request,String id){
        return webProductService.delete(id,acceptCurrentUser(request));
    }

    /**
     * 添加或修改
     *
     * @param webProductVo Vo对象
     * @author shengQiang yu
     * @date 2019-6-7 18:07:28
     */
    @BaseLog(module = "网站模块",className = "商品信息",methods = "添加或修改",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/webProduct/addOrUpdate",method = RequestMethod.POST)
    public ReturnEntity delete(HttpServletRequest request,WebProductVo webProductVo){
        return webProductService.addOrUpdate(webProductVo,acceptCurrentUser(request));
    }
}